/**************************************
* Number Converion(From decimal to others)
* Author: Yannis Zhao
* Date: 2014-8-15
*
***************************************/
#include <stdio.h>
#include <malloc.h>
/**********************Data Object Structure********************/
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10
#define ElemType int
typedef struct
{
ElemType *base;//Stack bottom pointer
ElemType *top;//Stack top pointer
int capacity;//Stack capacity
}SqStack;
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define null -65535
/***********************Operations******************************/
int InitStack(SqStack* stack);
void DestroyStack(SqStack* stack);
void ClearStack(SqStack* stack);
int Empty(SqStack stack);
int Size(SqStack stack);
ElemType PeekTop(SqStack stack);
int Push(SqStack* stack,ElemType e);
ElemType Pop(SqStack* stack);
void PrintStack(SqStack stack);
void PrintlnStack(SqStack stack);
int InitStack(SqStack* stack)
{
stack->base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));//Default size
if(!stack->base)//Malloc failed
return OVERFLOW;
stack->top=stack->base;
stack->capacity=STACK_INIT_SIZE;
return TRUE;
}
void DestroyStack(SqStack* stack)
{
free(stack->base);
stack->base=NULL;
stack->top=NULL;
stack->capacity=0;
}
void ClearStack(SqStack* stack)
{
stack->top=stack->base;
}
int Empty(SqStack stack)
{
if(stack.top==stack.base)
return TRUE;
return FALSE;
}
int Size(SqStack stack)
{
return stack.top-stack.base;
}
ElemType PeekTop(SqStack stack)
{
if(stack.top==stack.base)//Empty
return null;
return *(stack.top-1);
}
int Push(SqStack* stack,ElemType e)
{
if(stack->top-stack->base >= stack->capacity)//Stack is full
{
stack->base=(ElemType*)realloc(stack->base, (STACK_INIT_SIZE+STACK_INCREMENT)*sizeof(ElemType) );
if(!stack->base)
return OVERFLOW;
stack->top=stack->base+stack->capacity;
stack->capacity+=STACK_INIT_SIZE;
}
*stack->top++=e;//Set value and move to next position
return TRUE;
}
ElemType Pop(SqStack* stack)
{
if(stack->top==stack->base)//Empty
return null;
return *--stack->top;
}
void PrintStack(SqStack stack)
{
ElemType* p=NULL;
if(!stack.base)//Not Exist
{
printf("null\n");
return;
}
if(stack.base==stack.top)//Empty
{
printf("null");
return;
}
p=stack.base;
while(p<stack.top-1)
{
printf("%d ",*p++);
}
printf("%d",*p);
}
void PrintlnStack(SqStack stack)
{
ElemType* p=NULL;
if(!stack.base)//Not Exist
{
printf("null\n");
return;
}
if(stack.base==stack.top)//Empty
{
printf("null\n");
return;
}
p=stack.base;
while(p<stack.top-1)
{
printf("%d ",*p++);
}
printf("%d\n",*p);
}
int main(int argc , char** argv)
{
int n;
int r;//radix
SqStack stack;
InitStack(&stack);
printf("Enter radix:");
scanf("%d",&r);
while(r!=2 && r!=8 &&r!=10 && r!=16)
{
printf("Input not valid.\n");
printf("Enter radix:");
scanf("%d",&r);
}
printf("Enter a positive decimal interger:");
scanf("%d",&n);
while(n<0)
{
printf("You must enter a positive number.\n");
printf("Enter a positive decimal interger:");
scanf("%d",&n);
}
while(n)//Claculate and push
{
Push(&stack,n%r);
n=n/r;
}
if(16==r)//hexadecimal
{
while(!Empty(stack))//Pop
{
if(PeekTop(stack)>9)
{
printf("%c",Pop(&stack)+55);
}
else
printf("%d",Pop(&stack));
}
}
while(!Empty(stack))//Pop
{
printf("%d",Pop(&stack));
}
printf("\n");
return 0;
}
参考:《数据结构》清华大学出版社 严蔚敏 吴伟民
栈的应用——十进制数转其他进制
最新推荐文章于 2022-04-13 10:27:03 发布